package cn.com.zonesion.rfiddemo.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;

import cn.com.zonesion.rfiddemo.bean.InstructionBean;
import cn.com.zonesion.rfiddemo.R;

/**
 * ListView的适配器类
 */

public class InstructionListAdapter extends BaseAdapter {

    private Context context;//上下文对象
    private ArrayList<InstructionBean> instructionBeans;//存放InstructionBean对象的ArrayList

    public InstructionListAdapter(Context context, ArrayList<InstructionBean> instructionBeans) {
        this.context = context;
        this.instructionBeans = instructionBeans;
    }

    @Override
    public int getCount() {
        return instructionBeans.size();
    }

    @Override
    public Object getItem(int i) {
        return instructionBeans.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if(convertView == null){
            viewHolder = new ViewHolder();
            convertView = View.inflate(context, R.layout.list_log_layout,null);
            viewHolder.textInstruction = convertView.findViewById(R.id.text_instruction);
            viewHolder.textRole = convertView.findViewById(R.id.text_role);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }

        InstructionBean instructionBean = instructionBeans.get(i);
        viewHolder.textInstruction.setText(instructionBean.getInstruction());
        viewHolder.textRole.setText(instructionBean.getRole());

        return convertView;
    }

    public static class ViewHolder{
        TextView textInstruction;
        TextView textRole;
    }
}
